package cn.wx.scholar.neo4j.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.wx.common.persistence.exception.MyException;
import cn.wx.common.persistence.web.ResponseCode;
import cn.wx.scholar.core.service.ScholarFosService;
import cn.wx.scholar.neo4j.entity.Org;
import cn.wx.scholar.neo4j.entity.qo.ExpertQ;
import cn.wx.scholar.neo4j.repository.OrgRepository;
import cn.wx.scholar.tool.neo4j.Neo4jQueryTools;

@Service
public class OrgService extends Neo4jBaseService<Org> {
	
	Logger logger = LoggerFactory.getLogger(OrgService.class);
	
	@Autowired
    private OrgRepository orgRepository;
	
	@Autowired
	private ScholarFosService scholarFosService;
	
	public List<Org> getRelateOrg(Long id) {
		return orgRepository.getRelateOrg(id);
	}
	
	public List<Org> findByQueryParam(String name) {
		
		if(StringUtils.isBlank(name)) {
			
		}
		return orgRepository.findByQueryParam(Neo4jQueryTools.basicLike(name));
	}
	
	
	/**
	 * 统计全球热力分布图2
	 */
	public List<Org> loadOrgByQueryExperts2(ExpertQ expertQ) {
		
		Long st = System.currentTimeMillis();
		
		if(expertQ == null) {
			throw new MyException(ResponseCode.PARAMETER_REQUIRED);
		}
		
		/**
		 * 定义传参条件
		 */
		Map<String, Object> params = new HashMap<>();
		
		StringBuffer cypherBuffer = new StringBuffer();

		/**
		 * 用于领域的精确匹配
		 */
		final String cypher0 = " match(f:field)-[r:containse]->(p:expert)<-[r1:jobin]-(o:org) ";
		/**
		 * 用于全文检索
		 */
		final String cypher1 = " call apoc.index.search(\"locations\",\"";
		final String cypher2 =  "\",100000) yield node as p ,weight as w with p "
				+ " match(o:org)-[r:jobin]->(e:expert) where ID(e)=ID(p)";
		
		String locationsQ = expertQ.getName();
		
		if(StringUtils.isBlank(locationsQ)) {
			return null;
		}
		
		/**
		 * 处理查询，
		 * 如果 领域 是完整的 我们能搜索到，走精确匹配 
		 * 
		 * 先验证领域
		 */
		
		Long fosId = scholarFosService.queryFosId(locationsQ);
		
		
		/**
		 * 存在当前领域,走精确匹配
		 * 否则走模糊查询
		 */
		if(fosId != null && fosId > 0){
			cypherBuffer.append(cypher0).append(" where f.fId={fieldId} ");
			params.put("fieldId", fosId);
		}else{
			locationsQ = Neo4jQueryTools.basicIndex(locationsQ);
			cypherBuffer.append(cypher1).append(locationsQ).append(cypher2);
		}

		String where = " and p.nIndex>={min} and p.nIndex<{max} and p.name is not null ";
		
		cypherBuffer.append(where);
		
		params.put("min", expertQ.getMin());
		params.put("max", expertQ.getMax());
		
		
		/**
		 * 根据语言查询
		 */
		if (StringUtils.isNotBlank(expertQ.getLanguage())) {
			if ("other".equals(expertQ.getLanguage())) {
				cypherBuffer.append(" AND (p.language = '' or p.language is null)");
			} else {
				cypherBuffer.append(" AND p.language = {language}");
				params.put("language", expertQ.getLanguage());
			}

		}

		/**
		 * 根据国家查询
		 */
		if (StringUtils.isNotBlank(expertQ.getCountry())) {
			if ("other".equals(expertQ.getCountry())) {
				cypherBuffer.append(" AND (p.country = '' or p.country is null)");
			} else {
				cypherBuffer.append(" AND p.country = {country}");
				params.put("country", expertQ.getCountry());
			}
		}
		
		
		/**
		 * 根据5年 10年 查询
		 */
		if (expertQ.getPapers5yOr10y() != null) {
			 if (expertQ.getPapers5yOr10y() == 5){
				cypherBuffer.append(" AND p.papers5y > 0 ");
			} else if (expertQ.getPapers5yOr10y() == 10){
				cypherBuffer.append(" AND p.papers10y > 0 ");
			}
		}
		
		/**
		 * 查询List
		 */
		cypherBuffer.append(" RETURN distinct o limit 1000");
		String cypher = cypherBuffer.toString();
		
		List<Org> orgs = super.queryList(cypher, params);
		
		Long et = System.currentTimeMillis();
		
		logger.info("org running time :" + (et - st));
		
		return orgs;
	}
}
